/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tankproject;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class TankView extends JFrame {

    Tank tank = null;
    TankController controller = null;
    Turret turret = null;
    Image tankImage = null;
    Image turretImage = null;
    Image map = null;

    public TankView(Tank tank, Turret turret, TankController controller) {
        //initial values for tank
        try {
            this.map = ImageIO.read(new File("map.gif"));
        } catch (Exception ex) {
            System.out.println(ex);
        }

        try {
            this.tankImage = ImageIO.read(new File("tank1.gif"));
        } catch (Exception ex) {
            System.out.println(ex);
        }

        try {
            this.turretImage = ImageIO.read(new File("Turret.gif"));
        } catch (Exception ex) {
            System.out.println(ex);
        }

        this.tank = tank;
        this.controller = controller;
        this.turret = turret;
        this.addMouseListener(controller);
        this.addKeyListener(controller);
        this.addMouseMotionListener(controller);
        tank.setX(250);
        tank.setY(175);
        tank.setWidth(10);
        tank.setHeight(10);
    }

    @Override
    public void paint(Graphics g) {
        //move the tank across the screen
        if (tankImage == null) {
            tankImage = createImage(getWidth(), getHeight());
            System.out.println("Recreated");
            return;
        }

        if (turretImage == null) {
            turretImage = createImage(getWidth(), getHeight());
            System.out.println("Recreated");
            return;
        }

        AffineTransform tankRotate = new AffineTransform();
        AffineTransform turretRotate = new AffineTransform();

        Graphics2D bg = (Graphics2D) tankImage.getGraphics();
        Graphics2D g2d = (Graphics2D) g;
        Graphics2D g22d = (Graphics2D) g;
        //bg.drawImage(tankImage, (int) tank.getX(), (int) tank.getY(), this);
        g.drawImage(map, 0, 0, this);
        tankRotate.rotate(Math.toRadians(tank.rotationAngle), (tank.getX() + 56), (tank.getY() + 75));
        g2d.setTransform(tankRotate);
        g2d.drawImage(tankImage, (int) tank.getX(), (int) tank.getY(), this);
        turretRotate.rotate((turret.rotationAngle + Math.toRadians(90)), (tank.getX() + 60), (tank.getY() + 85));
        g22d.setTransform(turretRotate);
        g22d.drawImage(turretImage, (int) (tank.getX() - 0), (int) (tank.getY() - 30), this);

        ArrayList bullets = new ArrayList();
        for (int w = 0; w < bullets.size(); w++) {
            //This is how to get a current element in an arrayList
            //similar to x[2] in a normal array
            Bullet m = (Bullet) bullets.get(w);//draw:
            g2d.drawImage(m.getImage(), m.getX(), m.getY(), null);

        }
    }
}
